﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace GGJ.Sprites
{
    class EatingPlant : Ennemy
    {
        private int tileWidth = 80;

        private int tileHeight = 192;
        private double currentAnimationTime = 0.0d;
        Boolean eating = false;
        private Rectangle hitbox;

        public EatingPlant(Texture2D texture, int x, int y)
            : base(texture)
        {
            destination = new Rectangle(
                x,
                y,
                tileWidth,
                tileHeight);
        }

        public override void Update(GameTime gameTime, Flies flies, KilledFlies killedFlies, float speed, bool isSlowMotion)
        {

            destination.X -= (int)speed;

            currentAnimationTime += gameTime.ElapsedGameTime.TotalSeconds;

            float time = (isSlowMotion ? 1.5f : 1.0f);

            if (currentAnimationTime > time)
            {
                currentAnimationTime -= time;
                eating = !eating;
            }

            if (!eating)
            {
                hitbox = new Rectangle(destination.X + 10, destination.Y, tileWidth - 10, tileHeight / 3);

                Fly fly;

                for (int i = 0; i < flies.Count(); i++)
                {
                    fly = flies.GetFly(i);

                    if (fly != null)
                    {
                        if (hitbox.Intersects(fly.GetDestination()))
                        {
                            killedFlies.registeredLost();
                            flies.removeFly(i);
                        }
                    }
                }
            }
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            Rectangle source;

            if (eating)
            {
                source = new Rectangle(272+tileWidth,
               0,
               tileWidth,
               tileHeight);

             
            }
            else
            {
                source = new Rectangle(272,
                0,
                tileWidth,
                tileHeight);

             
            }

            spriteBatch.Draw(texture, destination, source, Color.White);
        }
    }
}
